//
//  LKBoTongFeedbackWebViewController+UserInfo.m
//  iOS-BTong-ApplicationCenter
//
//  Created by maqz on 2018/4/10.
//  Copyright © 2018年 毛毛驴. All rights reserved.
//

#import "LKBoTongFeedbackWebViewController+UserInfo.h"


@implementation LKBoTongFeedbackWebViewController (UserInfo)

NSString *BotongApplicationCenterUserInfoJSHandler = @"BotongApplicationCenterUserInfoJSHandler";

- (void)scriptUserInfoMessage:(WKScriptMessage *)message {
    if ([message.name isEqualToString:BotongApplicationCenterUserInfoJSHandler]) {
        /*
         *  本地用户信息 --- String
         *
         *  accessToken --- 请求Token
         *  refreshToken --- 刷新Token
         *  userId --- 账户编号（全局唯一）
         *  accountId --- 账号编号（修改账号信息专用）
         *  gender --- 性别；0：女；1：男
         *  birthday --- 生日
         *  area --- 地区
         *  profileColor --- 默认头 像颜色
         *  nick --- 昵称
         *  email --- 邮箱
         *  profile --- 头像
         *  mobile --- 电话
         *  position --- 职位
         *  memberId --- 企业成员编号
         *  companyId --- 企业编号
         *  companyName --- 公司名
         *  memberName --- 真实姓名
         */
        
        NSString *callBackMethod = message.body[scriptCallbackMethod];
        if (!callBackMethod || callBackMethod.length == 0) {
            return;
        }
        
        NSDictionary *userInfo = [[NSUserDefaults standardUserDefaults] objectForKey:@"LKBOTONGFEEDBACK_USERDEFAULTS_USERINFO"];
        
        NSString *userId = userInfo[@"userId"]?:@"";            // 用户id
        NSString *nick = userInfo[@"nick"]?:@"";                     // 昵称
        NSString *realName = userInfo[@"memberName"] ?: @"";    // 真实姓名
        NSString *profile = userInfo[@"profile"]?:@"";               // 头像
        NSString *mobile = userInfo[@"mobile"]?:@"";                 // 电话
        NSString *appId = userInfo[@"appId"]?:@"";
        
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; //app版本号
        NSString *systemVersion = [[UIDevice currentDevice] systemVersion]; //操作系统版本
        NSString *deviceModel = [[UIDevice currentDevice] model]; //设备型号
        
        NSDictionary *params = @{
                                 @"userId"      : userId,
                                 @"nick"        : nick,
                                 @"realName"    : realName,
                                 @"profile"     : profile,
                                 @"mobile"      : mobile,
                                 @"deviceModel" : deviceModel,
                                 @"systemName"  : @"IOS",
                                 @"systemVersion" : systemVersion,
                                 @"appVersion"  : appVersion,
                                 @"appId"       : appId
                                 };
        
        NSString *paramsJson = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
        paramsJson = [paramsJson stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        paramsJson = [paramsJson stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        NSString *scriptString = [NSString stringWithFormat:@"%@(%@)", callBackMethod, paramsJson];
        [self evaluateJavaScript:scriptString completionHandler:nil];
    }
}

@end
